﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Wheeler.Common.Extensions;
using Wheeler.Domain;
using Wheeler.Web.ViewModels;
using Wheeler.Web.Utility;
using AutoMapper;
using Wheeler.Web.ViewModels.UtilityModels;

namespace Wheeler.Web.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin")]
    public class AccountController : BaseController
    {
		private WheelerEntities _Context = new WheelerEntities();
        
		#region Constructors
		public AccountController()
		{
		}

		public AccountController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        
        public ActionResult DashBoard()
        {
            return View(LoggedInUser);
        }

        public ActionResult BackupDb()
        {
            return View();
        }

        /// <summary>
        /// Backs up the currently using database and stores in a downloadable location
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public String GenerateBackup()
        {
            String directory = Server.MapPath(AppConstants.Directories.TEMP);
            if(!System.IO.Directory.Exists(directory))
                System.IO.Directory.CreateDirectory(directory);

            String fileName = System.IO.Path.Combine(directory
                , String.Format("3WCNG_DB_{0}.sql", DateTime.Now.ToString("dd-MM-yyyy hhmmtt")));



            UtilityDAO dao = new UtilityDAO();
            dao.BackupDatabase(fileName);

            return String.Format("{0}/{1}", AppConstants.Directories.TEMP, System.IO.Path.GetFileName(fileName)).UrlEncode();
        }

        public ActionResult DownloadBackup(String file)
        {
            
            String tempDirectory = Server.MapPath(AppConstants.Directories.TEMP);
            
            String fileName = Server.MapPath(file.UrlDecode());

            ///Files without the temp directory and only *.sql files can be downloaded through this controller
            if (String.Compare(tempDirectory, System.IO.Path.GetDirectoryName(fileName), true) != 0
                || System.IO.Path.GetExtension(fileName).ToLower() != ".sql" )
                throw new ArgumentException(String.Format("File {0} Cannot be downloaded", file.UrlDecode()));

            ///Intentionally keeping the temp database backup file.
            return File(fileName, "text/plain", System.IO.Path.GetFileName(fileName));
            
        }

        //[HttpPost]
        //public String GenerateFullBackup()
        //{
        //    String directory = Server.MapPath(AppConstants.Directories.TEMP);
        //    if (!System.IO.Directory.Exists(directory))
        //        System.IO.Directory.CreateDirectory(directory);

        //    String fileName = System.IO.Path.Combine(directory
        //        , String.Format("3WCNG_DB_{0}.sql", DateTime.Now.ToString("dd-MM-yyyy hhmmtt")));



        //    UtilityDAO dao = new UtilityDAO();
        //    dao.BackupDatabase(fileName);

        //    ///Zip Contents of Uploaded Image directory for backup purpose
        //    String imageDirectory = Server.MapPath(AppConstants.Directories.VEHICLE_PICTURE);
        //    if (!System.IO.Directory.Exists(imageDirectory))
        //        System.IO.Directory.CreateDirectory(imageDirectory);

        //    System.IO.

        //    return String.Format("{0}/{1}", AppConstants.Directories.TEMP, System.IO.Path.GetFileName(fileName)).UrlEncode();
        //}
        //public ActionResult DownloadFullBackup(String file)
        //{

        //}

		#endregion Action Methods
		
		#region Helper Methods		

        #endregion			
    }
}


